package org.blueoxygen.cimande2.ma.gene;

import java.util.ArrayList;

import org.blueoxygen.cimande2.ma.gene.adapter.PersonAdapter;
import org.blueoxygen.cimande2.ma.gene.entity.Person;
import org.blueoxygen.cimande2.ma.gene.manager.ConnectionManager;
import org.blueoxygen.cimande2.ma.gene.manager.DbManager;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Parent extends ListActivity {
	
	private int selectedItemId;
	private ConnectionManager connectionManager;
	private ArrayList<Person> persons = new ArrayList<Person>();
	private Person father, mother;
	private Person dataDiriku, fromIntent;
	private DbManager dbManager;
	private Person person;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.list);
		getListView().setClickable(true);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		TextView listTitle = (TextView) findViewById(R.id.listTitle);
		listTitle.setText("   Parent");
		ImageView imageView = (ImageView) findViewById(R.id.ImageView01);
		imageView.setBackgroundResource(R.drawable.people);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
		
		
		connectionManager = new ConnectionManager();
		dbManager = new DbManager(this);
		Bundle bundle = getIntent().getExtras();
		fromIntent = (Person) bundle.getSerializable("myprofile");
		dataDiriku = connectionManager.getPersonalData(dbManager
				.getConnectionUrl()
				+ "Cimande2/person/" + fromIntent.getId() + "/.json");

		persons = new ArrayList<Person>();
		father = dataDiriku.getFather();
		persons.add(father);
		mother = dataDiriku.getMother();
		persons.add(mother);

		PersonAdapter personAdapter = new PersonAdapter(this, persons);
		getListView().setAdapter(personAdapter);
		
		getListView().setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				selectedItemId = getListView().getCheckedItemPosition();
				Intent intent = new Intent(Parent.this, Profile.class);
				person = new Person();
				person = connectionManager.getPersonalData(dbManager
						.getConnectionUrl()
						+ "Cimande2/person/" + getIdList(selectedItemId) + "/.json");
				intent.putExtra("myprofile", person);
				startActivity(intent);
			}
		});
		findViewById(R.id.homeBtn).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Parent.this, DashBoard.class);
				startActivity(intent);
			}
		});
		findViewById(R.id.preferenceBtn).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Parent.this, Preference.class);
				startActivity(intent);
			}
		});
	}

	private String[] getNameList() {
		String data[] = new String[persons.size()];
		int index = 0;
		for (Person p : persons) {
			data[index] = p.getFirstName();
			index++;
		}
		return data;
	}

	private String getIdList(int selectedIndex) {
		int index = 0;
		String data[] = new String[persons.size()];
		for (Person p : persons) {
			data[index] = p.getId();
			index++;
		}
		return data[selectedIndex];
	}
}
